/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import EmbeddedBridge from 'libembeddedbridge.so'
import { XComponent } from '../../cpp/types/libembeddedbridge/Index'

export default class EmbeddedBridgeNAPI {
  private static instance: EmbeddedBridgeNAPI;

  static getInstance(): EmbeddedBridgeNAPI {
    if(EmbeddedBridgeNAPI.instance == null) {
      EmbeddedBridgeNAPI.instance = new EmbeddedBridgeNAPI();
    }
    return EmbeddedBridgeNAPI.instance;
  }
  //ArkTS调用C++接口

  dispatchMessage(handler: string, fun: string, param: string) {
    EmbeddedBridge.dispatchMessage(handler, fun, param);
  }
  /*  功能：监听C++消息, 可以是来自非JS线程消息
      参数：id 注册的安全函数名
           callback 接收消息的回调
  */
  onMessageThreadSafe(id: string, callback:(handler: string, fun: string, data: string)=> ESObject) {
    EmbeddedBridge.registerThreadSafeFun(id, callback);
  }
  //ArkTS调用C++接口
  registerXComponent(params: XComponent) {
    EmbeddedBridge.registerXComponent(params);
  }
}